/**********************************************************************
 * View: Calendar
 *
#-~>weboo:class<~-#
 */ 
(function($){
var CSS_DAY_BLANK	= "wdi_blank";
var CSS_DAY_CURRENT	= "wdi_current";
var CSS_DAY			= "wdi";
$.weboo.view("weboo.date.calendar",null,null,{setup: function(view){
	view.patternName = "weboo.pattern.date.calendar";
	view.speed = {show:10,hide:400};
	
	view.beforeRender = function(){
		var me = this.pattern().render();
		var cells = me.find("[plugid='day'] td");
		if(cells.size()!=49) throw "Dataview need a 7x7 table as pattern!";
		// make sure the values is a pure date object
		if(!$.weboo.isDate(this.values)) this.values = $.weboo.date().getDate();
		if(this.values._ily) this.values = this.values.getDate();
		/*
		 * render dom
		 */
		var date = $.weboo.date(this.values);
		me.find("[plugid='month']").val(date.month);
		me.find("[plugid='year']").val(date.year);
		var first = 7 + date.getMonthFirstWeekDay();
		var max = date.getMaxDayOfMonth();
		for(var i=7;i<first;i++){
			$(cells[i]).addClass(CSS_DAY_BLANK);
		}
		for(var i=0;i<max;i++){
			var d = i+1;
			var c = $(cells[i+first]);
			c.text(d);
			if(d == date.day)
				c.addClass(CSS_DAY_CURRENT);
			else
				c.addClass(CSS_DAY);
		}
		for(var i = max+first;i<49;i++){
			$(cells[i]).addClass(CSS_DAY_BLANK);
		}
		/*
		 * render events
		 */

		return me;
	};

	view.blank = function(){};

	view.afterRender = function(){
		var days = this.find("[plugid='day']").find("td:gt(6):not([class='"+CSS_DAY_BLANK+"'])");
		days.bind("click",{view:this,fn:"onClickDay"},$.weboo.fire);
		days.bind("dblclick",{view:this,fn:"onDblClickDay"},$.weboo.fire);
		this.find("[plugid='year']").bind("change",{view:this,fn:"onChange"},$.weboo.fire);
		this.find("[plugid='month']").bind("change",{view:this,fn:"onChange"},$.weboo.fire);
	};

	view.onChange = function(){
		this.data($.weboo.date(this.data()));
		this.redraw();
		if($.weboo.isView(this.gasket) && this.gasket.bindSubEvents())
			this.gasket.bindSubEvents();
	};

	view.onClickDay = function(event,params){
		var old = this.find("[plugid='day']").find("td[class='"+CSS_DAY_CURRENT+"']");
		old.removeClass(CSS_DAY_CURRENT).addClass(CSS_DAY);
		$(event.target).removeClass(CSS_DAY).addClass(CSS_DAY_CURRENT);
	};

	view.onDblClickDay = function(event,params){
		this.onClickDay(event,params);
		this.gasket.ok();
	};

	view.data = function(d){
		if(!$.weboo.undefined(d)){
			this.values = $.weboo.date(d);
			return this;
		}
		var yy = $.weboo.val(this.find("[plugid='year']").get(0));
		var mm = $.weboo.val(this.find("[plugid='month']").get(0));
		var dd = $.weboo.val(this.find("[plugid='day']").find("td[class='"+CSS_DAY_CURRENT+"']").get(0));
		return $.weboo.date(yy+"-"+mm+"-"+dd).toRegularString();
	};
}});
})(jQuery);
/**********************************************************************
 * View: Date Picker
 *
#-~>weboo:class<~-#
 */ 
(function($){
$.weboo.view("weboo.date.picker",null,null,{setup: function(view,conf){
	view.patternName = "weboo.pattern.date.picker";

	view.combine = new Array({
		name:"main",type:"weboo.date.calendar",to:"calendar"
		});

	view.bindSubEvents = function(){
		this.firstChild("calendar").find("[plugid='close']").bind("click",{view:this,fn:"ok"},$.weboo.fire);
	};

	view.afterRender = function(){
		var btn = this.find("[plugid='show']");
		btn.bind("mousedown",{view:this,fn:"edit"},$.weboo.fire);
		btn.bind("click",null,$.weboo.stopEvent);
		this.find("[plugid='clear']").bind("click",{view:this,fn:"blank"},$.weboo.fire);
		this.jq.bind("click",null,$.weboo.stopEvent);
		this.jq.bind("mousedown",null,$.weboo.stopEvent);
		$(document.body).bind("click",{view:this,fn:"close"},$.weboo.fire);
		this.bindSubEvents();
	};

	view.edit = function(){
		var c = this.firstChild("calendar");
		c.data(this.data($.weboo.date(this.val("box"))));
		this.find("[plugid='calendar']").show();
		c.redraw();
		this.bindSubEvents();
	};

	view.blank = function(){
		this.values.clear();
		this.close();
		this.find("[plugid='box']").text("");
	};

	view.apply = function(){
		this.find("[plugid='box']").text(this.firstChild("calendar").data());
	}

	view.close = function(){
		this.find("[plugid='calendar']").fadeOut(this.firstChild("calendar").speed.hide);
	};

	view.ok = function(){
		this.apply();
		this.close();
	};

	view.data = function(d){
		var c = this.firstChild("calendar");
		if($.weboo.undefined(d)){
			return this.val("box");
		}
		else{
			c.data(d);
			this.values.set("box",d);
		}
		return this;
	};
}}); // ~ end picker setup
})(jQuery);